<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-09 20:41:17
 * @LastEditors: 任飞
 * @LastEditTime: 2020-07-02 23:14:58
 -->
<template>
  <div class="app-container">
    <div class="head-container">
      <el-button type="primary" size="mini" class="filter-item" @click="$refs.form.dialog = true">新增</el-button>
    </div>

    <el-table v-loading="loading" ref="table" :data="data" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" row-key="id" element-loading-text="拼命加载中" border fit highlight-current-row>
      <el-table-column label="角色代码" prop="roleCode" align="left" />
      <el-table-column label="角色名称" prop="roleName" align="center" />
      <el-table-column label="操作" align="center" width="500px">
        <template slot-scope="scope">
          <!-- <edit :data="scope.row" :sup_this="sup_this" /> -->
          <!-- 角色选择菜单权限 -->
          <el-button size="mini" type="warning" @click="displayManage(scope.row.roleCode)">管理资源</el-button>
          <el-button size="mini" type="danger" @click="subDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <menu-manage ref="menuManage" />

    <action-manage ref="actionManage" />

    <user-manage ref="userManage"/>
    <eForm ref="form" :is-add="true" :sup_this="sup_this"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import MenuManage from './module/menuManage'
import ActionManage from './module/actionManage'
import UserManage from './module/userManage'
import TreeTable from './module/treeTable'
import edit from './module/edit'
import add from './module/add'
import eForm from './module/form'

export default {
  components: {
    MenuManage,
    ActionManage,
    UserManage,
    TreeTable,
    edit, eForm, add
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        "0": "success",
        "1": "danger"
      };
      return statusMap[status];
    },
    statusMap(status) {
      const statusMap = {
        "0": "正常",
        "1": "停用"
      };
      return statusMap[status];
    },
    typeFilter(type) {
      const typeMap = {
        "0": "gray",
        "1": "primary"
      };
      return typeMap[type];
    },
    typeMap(type) {
      const typeMap = {
        "0": "成员",
        "1": "群组"
      };
      return typeMap[type];
    }
  },
  mixins: [initData],
  data() {
    return {
      sup_this: this,
      statusOptions: [
        {
          label: '正常',
          value: 0
        },
        {
          label: '停用',
          value: 1
        }
      ],
      typeOptions: [
        {
          label: '成员',
          value: 0
        },
        {
          label: '群组',
          value: 1
        }
      ],
      delLoading: false,
      tableQuery: {
        limit: 10,
        current_page: 1,
        name: null,
        status: null,
        type: null,
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  mounted() {
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = '/admin/sysRole/listPage'
      const query = this.query
      const value = query.value
      this.params = { pageNum: this.page, pageSize: this.limit }
      if (value) { this.params['name'] = value }
      return true
    },
    handleFilter() {
      this.init()
    },
    handleReset() {
      this.tableQuery.limit = 10;
      this.tableQuery.current_page = 1;
      this.tableQuery.name = null;
      this.tableQuery.status = null;
      this.tableQuery.type = null;

      this.init()
    },
    subDelete(id) {
      this.delLoading = true
      this.$api.fenxiao.role.delRole(id).then(res => {
        this.delLoading = false
        this.init()
        this.$notify({ title: '删除成功', type: 'success', duration: 2500 })
      }).catch(err => {
        this.delLoading = false
        console.log(err.response.data.message)
      })
    },
    displayManage(code) {
      this.$refs.menuManage.getRoleMenu(code)
    },
    handleAddRole() {
      this.$api.fenxiao.role.add()
        .then()
        .catch()
    }
  }
};
</script>
